package reduce.fz.com.reduce.fragment;


import android.content.Intent;
import android.os.Bundle;
import android.app.Fragment;
import android.support.v7.widget.LinearLayoutManager;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.alibaba.fastjson.TypeReference;

import java.util.List;

import reduce.fz.com.reduce.R;
import reduce.fz.com.reduce.abstracts.AbResponseListener;
import reduce.fz.com.reduce.activity.UserDetailActivity;
import reduce.fz.com.reduce.adapter.BaseRecycleViewHolder;
import reduce.fz.com.reduce.bean.ServiceException;
import reduce.fz.com.reduce.bean.User;
import reduce.fz.com.reduce.bean.page.Pagination;
import reduce.fz.com.reduce.bean.result.Result;
import reduce.fz.com.reduce.custom.view.RoundNetworkImageView;
import reduce.fz.com.reduce.fragment.base.BaseListFragment;
import reduce.fz.com.reduce.util.ApiUtil;
import reduce.fz.com.reduce.util.HttpUtils;

/**
 * A simple {@link Fragment} subclass.
 */
public class SocietyMemberFragment extends BaseListFragment<SocietyMemberFragment.SocietyMemberViewHolder, User> {
    private Long sid;


    public interface SocietyMemberListener {

        public void memberClick(User user);
    }

    public static SocietyMemberFragment create(Long sid) {
        SocietyMemberFragment fragment = new SocietyMemberFragment();
        Bundle bundle = new Bundle();
        bundle.putLong("sid", sid);
        fragment.setArguments(bundle);
        return fragment;
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        sid = getArguments().getLong("sid");
    }

    public SocietyMemberFragment() {
    }

    @Override
    public LinearLayoutManager getLayoutManager() {
        return null;
    }

    public void forwardUserDetail(User user) {
        Intent intent = new Intent(getActivity(), UserDetailActivity.class);
        intent.putExtra("uid", user.uid);
        startActivity(intent);
    }

    @Override
    protected SocietyMemberViewHolder createItemViewHolder(ViewGroup parent) {
        View view = layoutInflater.inflate(R.layout.view_society_member, parent, false);
        SocietyMemberViewHolder viewHolder = new SocietyMemberViewHolder(view, new SocietyMemberListener() {
            @Override
            public void memberClick(User user) {
                forwardUserDetail(user);
            }
        });
        return viewHolder;
    }

    @Override
    protected SocietyMemberViewHolder createfootViewHolder(View view) {
        return new SocietyMemberViewHolder(view);
    }

    @Override
    public void request(String url) {
        HttpUtils.get(url, new AbResponseListener<List<User>>(new TypeReference<Result<List<User>>>() {
        }) {
            @Override
            public void error(ServiceException e) {
                loadingDialog.dismiss();
            }

            @Override
            public void responseInfo(Result<List<User>> data) {
                addResult(data);
            }
        });
    }

    @Override
    protected String setUrlBeforeRequest(List<User> datas, Pagination pagination) {
        String url = ApiUtil.getApi(context, R.array.api_society_member,sid, pagination.getPageNo(), pagination.getPageSize());
        return url;
    }

    @Override
    protected int getPageType() {
        return 0;
    }

    public static class SocietyMemberViewHolder extends BaseRecycleViewHolder<User> {
        private RoundNetworkImageView matavar;
        private TextView mintr;
        private TextView mNickname;
        private SocietyMemberListener listener;
        private View itemView;

        public SocietyMemberViewHolder(View itemView, SocietyMemberListener listener) {
            this(itemView);
            this.itemView = itemView;
            this.listener = listener;

        }

        public SocietyMemberViewHolder(View itemView) {
            super(itemView);
            matavar = (RoundNetworkImageView) itemView.findViewById(R.id.iv_user_avatar);
            mintr = (TextView) itemView.findViewById(R.id.tv_intr);
            mNickname = (TextView) itemView.findViewById(R.id.tv_user_nickname);
        }

        @Override
        public void renderView(final User data, int position) {
            matavar.setImageUrl(data.avatar, HttpUtils.getImageLoader());
            mintr.setText("一共发表了"+data.userAttach.works_num+"个作品");
            mNickname.setText(data.nickname);
            this.itemView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    listener.memberClick(data);
                }
            });
        }
    }


}
